package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.PermVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;


import javax.management.relation.Role;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/role")
public class DtsAdminRoleController {

    @Autowired
    DtsRoleService dtsRoleService;
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    DtsPermissionService permissionService;


    //授权
    @PostMapping("/permissions")
    public Object grantPermission(@RequestBody Map<String,Object> map){
        Integer roleId = (Integer) map.get("roleId");
        ArrayList<String> permissions = (ArrayList) map.get("permissions");
        if(dtsRoleService.isSuperAdmin(roleId)){
            return ResponseUtil.fail(-1,"超级管理员不能修改权限");
        }
        permissionService.updatePermission(roleId,permissions);
        return ResponseUtil.ok();
    }



    //查权限列表
    @RequiresPermissions("admin:role:permission:get")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "权限详情")
    @GetMapping("/permissions")
    public Object findPermissionById(Integer roleId){
        List<Permission> permissions = PermissionUtil.listPermission(applicationContext, "com.qf.controller");
        Set<String> permissionStrs = PermissionUtil.listPermissionString(permissions);
        List<PermVo> systemPermissions = PermissionUtil.listPermVo(permissions);
        Map<String,Object> data = new HashMap<>();
        data.put("systemPermissions",systemPermissions);


        if(dtsRoleService.isSuperAdmin(roleId)){
            data.put("assignedPermissions",permissionStrs);
        }else {
            Set<String> perms = permissionService.findPermissionByIds(new Integer[]{roleId});
            data.put("assignedPermissions",perms);
        }
        return ResponseUtil.ok(data);
    }






    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsRole dtsRole){
        boolean flag=dtsRoleService.delete(dtsRole);
        if(flag){
            return ResponseUtil.ok();
        }else {
            return ResponseUtil.fail(-1,"该角色不能删除");
        }
    }


    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole dtsRole){
        DtsRole role=dtsRoleService.update(dtsRole);
        return ResponseUtil.ok(role);
    }


    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsRole dtsRole){
        DtsRole role=dtsRoleService.add(dtsRole);
        if(role==null){
            return ResponseUtil.fail(-1,"用户名已重复");
        }else {
            return ResponseUtil.ok(role);
        }


    }



    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "角色管理" }, button = "角色查询")
    @GetMapping("/list")
    public Object list(@RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(name = "limit",required = false,defaultValue = "10")Integer limit,
                       String sort,String order,String rolename){
        IPage<DtsRole> iPage=dtsRoleService.findByPage(page,limit,sort,order,rolename);
        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }
    @GetMapping("/options")
    public Object options(){
        List<DtsRole> list= dtsRoleService.findAll();
        List<Map<String, Object>> data = list.stream().map(Role -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", Role.getId());
            map.put("label", Role.getName());
            return map;
        }).collect(Collectors.toList());
        return ResponseUtil.ok(data);
    }
}
